#include "gameselect.h"
#include <QPainter>
#include <QPixmap>
#include <QPushButton>
#include "gamesnake.h"
#include "gameroom.h"
#include <QSound>
#include <QTextEdit>
#include <QFile>
#include <QTextStream>//文本流对象
GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(1000,800);
    //设置窗口图标
    this->setWindowIcon(QIcon(":res/ico.png"));
    //设置窗口标签
    this->setWindowTitle("选择大厅");
   //实现游戏房间和选择关卡切换
    //创建另一个窗口
    GameRoom *room=new GameRoom();


    //创建按钮，给按钮设置格式
    //容易关卡
   QPushButton *briefnessBtn= new QPushButton(this);
   briefnessBtn->setStyleSheet("QPushButton{background-color:#0072C6;color: black; border:0px groove gray;border-radius:10px;padding:6px;}");
   briefnessBtn->move(400,200);
   QFont font("华文行楷",25);
   briefnessBtn->setFont(font);
   briefnessBtn->setText("容易模式");
   connect(briefnessBtn,&QPushButton::clicked,[=]{
       //关闭上个窗口
       this->close();
       room->setTimer(200);
       room->setGeometry(this->geometry());
       room->show();
       QSound::play(":res/clicked.wav");
  });


   //正常关卡
  QPushButton *normalBtn= new QPushButton(this);
  normalBtn->setStyleSheet("QPushButton{background-color:#0072C6;color: black; border:0px groove gray;border-radius:10px;padding:6px;}");
  normalBtn->move(400,280);
  QFont font1("华文行楷",25);
  normalBtn->setFont(font);
  normalBtn->setText("正常模式");
  connect(normalBtn,&QPushButton::clicked,[=]{
      //关闭上个窗口
      this->close();
    room->setTimer(100);
      room->setGeometry(this->geometry());
      room->show();
      QSound::play(":res/clicked.wav");

 });

  //困难关卡
 QPushButton *difficultyBtn= new QPushButton(this);
 difficultyBtn->setStyleSheet("QPushButton{background-color:#0072C6;color: black; border:0px groove gray;border-radius:10px;padding:6px;}");
 difficultyBtn->move(400,360);
 QFont font2("华文行楷",25);
 difficultyBtn->setFont(font);
 difficultyBtn->setText("困难模式");
 connect(difficultyBtn,&QPushButton::clicked,[=]{
     //关闭上个窗口
     this->close();
    room->setTimer(80);
     room->setGeometry(this->geometry());
     room->show();
     QSound::play(":res/clicked.wav");

});

 //地狱关卡
QPushButton *bhellBtn= new QPushButton(this);
bhellBtn->setStyleSheet("QPushButton{background-color:#0072C6;color: black; border:0px groove gray;border-radius:10px;padding:6px;}");
bhellBtn->move(400,440);
QFont font3("华文行楷",25);
bhellBtn->setFont(font);
bhellBtn->setText("地狱模式");
connect(bhellBtn,&QPushButton::clicked,[=]{
    //关闭上个窗口
    this->close();
    room->setTimer(30);
    room->setGeometry(this->geometry());
    room->show();
    QSound::play(":res/clicked.wav");

});

//排行榜
QPushButton *ranpingBtn= new QPushButton(this);
ranpingBtn->setStyleSheet("QPushButton{background-color:#0072C6;color: black; border:0px groove gray;border-radius:10px;padding:6px;}");
ranpingBtn->move(400,520);
QFont font4("华文行楷",25);
ranpingBtn->setFont(font);
ranpingBtn->setText("历史战绩");
//创建一个信号槽
connect(ranpingBtn,&QPushButton::clicked,[=]{
    //创建一个窗口控件
    QWidget *wGet=new QWidget();
    wGet->setWindowTitle("历史战绩");
    wGet->setFixedSize(500,300);
    //创建一个多行输入框
    QTextEdit *edit=new QTextEdit(wGet);
    edit->setFont(font);
    edit->setFixedSize(500,300);
    //打开文件,并且只能读，文件要自己创建
    QFile file("D:/QT_creator_3_25/greedy_snake1/Snake/res/1.txt");
    file.open(QIODevice::ReadOnly);
    //文本流对象
    QTextStream in(&file);
    //in.表示文件，把文件里的数据按行读取，并且转化为整数
    int data=in.readLine().toInt();
    edit->append("得分为:");
    //这里需要的是QString类型,把整形转化为字符串
    edit->append(QString::number(data));
    //显示窗口
    wGet->show();
    QSound::play(":res/clicked.wav");
    file.close();
});

//返回按钮
QPushButton *returnBtn= new QPushButton(this);
returnBtn->move(900,700);
returnBtn->setIcon(QIcon(":res/up.png"));

connect(returnBtn,&QPushButton::clicked,[=]{
    this->close();
    GameSnake *snake= new GameSnake();
    snake->show();
    QSound::play(":res/clicked.wav");
        });

}

void GameSelect::paintEvent(QPaintEvent *event)
{
    //设置窗口大小
    this->setFixedSize(1000,800);
    //创建绘画者
    QPainter paint(this);
    //创建画板
    QPixmap pix(":res/game_select.png");
    paint.drawPixmap(0,0,this->width(),this->height(),pix);
}
